package com.niuxiaofa.newlaw.Activity;

import android.app.SearchManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.niuxiaofa.newlaw.Base.BaseActivity;
import com.niuxiaofa.newlaw.Bean.BaseBean;
import com.niuxiaofa.newlaw.Bean.QuestionBankBean;
import com.niuxiaofa.newlaw.Bean.SignInBean;
import com.niuxiaofa.newlaw.Bean.SignPageBean;
import com.niuxiaofa.newlaw.Bean.TimeofAssessmentBean;
import com.niuxiaofa.newlaw.Network.ApiService;
import com.niuxiaofa.newlaw.Network.ServiceManager;
import com.niuxiaofa.newlaw.Network.WisdomLibraryPresenter;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.ActivityUtil;
import com.niuxiaofa.newlaw.Tools.UIUtils;
import com.niuxiaofa.newlaw.View.AnswerTipDialog;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import www.linwg.org.lib.LCardView;


/**
 * 答题
 */
public class AnswerQuestionsActivity extends BaseActivity {


    @BindView(R.id.topic)
    TextView topic;

    @BindView(R.id.card1)
    LCardView lCardView1;

    @BindView(R.id.card2)
    LCardView lCardView2;

    @BindView(R.id.card3)
    LCardView lCardView3;


    @BindView(R.id.submit)
    LCardView submit;


    @BindView(R.id.text1)
    TextView textView1;


    @BindView(R.id.text2)
    TextView textView2;


    @BindView(R.id.text3)
    TextView textView3;

    String id;
    String userAnswers = "";

    @BindView(R.id.daojishi)
    TextView daojishi;



    @Override
    protected int getLayoutId() {
        return R.layout.activity_answer_question;
    }

int count=60;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==100){
                if(count>0){
                    count--;
                    daojishi.setText("总倒计时"+count);
                    handler.sendEmptyMessageDelayed(100,1000);
                }else {
                    ActivityUtil.getInstance().finishCurrentActivity();
                }
            }
        }
    };


    @Override
    protected void initData() {
        setTitle("正式答题");

        handler.sendEmptyMessageDelayed(100,1000);

        WisdomLibraryPresenter.getQuestionBank(new WisdomLibraryPresenter.interface_getQuestionBank() {
            @Override
            public void getResponse(QuestionBankBean u) {

                if (u.getCode() == 1) {
                    topic.setText(u.getData().getQuestions());
                    textView1.setText(u.getData().getAnswer().split("_")[0]);
                    textView2.setText(u.getData().getAnswer().split("_")[1]);
                    textView3.setText(u.getData().getAnswer().split("_")[2]);
                    id = u.getData().getId() + "";

                }
            }
        });


        lCardView1.setOnClickListener(e -> {
            userAnswers = "A";
            lCardView1.setCardBackgroundColor(Color.parseColor("#FFFED94D"));
            lCardView2.setCardBackgroundColor(Color.parseColor("#FFF1F1F6"));
            lCardView3.setCardBackgroundColor(Color.parseColor("#FFF1F1F6"));
        });


        lCardView2.setOnClickListener(e -> {
            userAnswers = "B";
            lCardView1.setCardBackgroundColor(Color.parseColor("#FFF1F1F6"));
            lCardView2.setCardBackgroundColor(Color.parseColor("#FFFED94D"));
            lCardView3.setCardBackgroundColor(Color.parseColor("#FFF1F1F6"));
        });


        lCardView3.setOnClickListener(e -> {
            userAnswers = "C";
            lCardView1.setCardBackgroundColor(Color.parseColor("#FFF1F1F6"));
            lCardView2.setCardBackgroundColor(Color.parseColor("#FFF1F1F6"));
            lCardView3.setCardBackgroundColor(Color.parseColor("#FFFED94D"));
        });

        submit.setOnClickListener(e -> {

            Map map = new HashMap();

            map.put("id", id);
            map.put("userAnswers", userAnswers);

            Gson gson = new Gson();

            String json = gson.toJson(map);

            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);


            Call<BaseBean> call = ServiceManager.getInstance().creat(ApiService.class).getUserAnswers(requestBody);

            call.enqueue(new Callback<BaseBean>() {
                @Override
                public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                    //判断请求是否成功
                    if (response.isSuccessful()) {

                        BaseBean baseBean = (BaseBean) response.body();


                        AnswerTipDialog answerTipDialog = new AnswerTipDialog(UIUtils.getActivity(), (boolean) baseBean.getData(), new DialogInterface.OnCancelListener() {
                            @Override
                            public void onCancel(DialogInterface dialogInterface) {

                            }
                        });
                        answerTipDialog.show();


                    }
                }

                @Override
                public void onFailure(Call<BaseBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());
                    Toast.makeText(UIUtils.getActivity(), "网络连接异常,请稍候在试", Toast.LENGTH_LONG).show();
                }
            });
        });


    }

}